package com.manager.plat.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import com.manager.plat.dao.IRouterDao;
import com.manager.plat.dao.IGatewayDao;
import com.manager.plat.dao.IPartnerLowerDao;
import com.manager.plat.vo.GatewayVo;
import com.manager.plat.vo.PartnerLowerVo;
import com.manager.plat.vo.RouterVo;
import com.manager.plat.vo.UserInfoVo;
import com.opensymphony.xwork2.ActionSupport;

public class RouterAction extends ActionSupport {
	/**
	 * 
	 */
	private static final long serialVersionUID = -2587636782201781391L;

	private Logger logger = Logger.getLogger(RouterAction.class);
	
	private IRouterDao routerDao;					//渠道路由信息数据接口
	private IPartnerLowerDao partnerLowerDao;		//渠道信息数据接口
	private IGatewayDao gatewayDao;					//业务配置信息数据接口
	private List<RouterVo> routerVoList;			//渠道路由信息列表
	private List<PartnerLowerVo> partnerLowerVoList;//渠道信息列表
	private List<GatewayVo> gatewayVoList;			//业务配置信息列表
	private RouterVo routerVo;						//渠道路由信息VO
	private String actionPath;						//操作成功跳转路径
	
	public String getRouterList(){
		routerVoList = routerDao.getRouterList();
		partnerLowerVoList = partnerLowerDao.getPartnerLowerList();
		return SUCCESS;
	}
	
	public String addRouter(){
		routerVo = null;
		partnerLowerVoList = partnerLowerDao.getPartnerLowerList();
		gatewayVoList = gatewayDao.getGatewayList();
		return SUCCESS;
	}
	
	public String editRouter(){
		if(routerVo.getId() != 0){
			routerVo = routerDao.getRouterById(routerVo.getId());
		}
		
		partnerLowerVoList = partnerLowerDao.getPartnerLowerList();
		gatewayVoList = gatewayDao.getGatewayList();
		return SUCCESS;
	}
	
	public String updateRouter(){
		HttpServletRequest request = ServletActionContext.getRequest();
		UserInfoVo userInfoVo = (UserInfoVo) request.getAttribute("user");
		
		/**获得网关信息ID*/
		int gateway_id = routerVo.getGateway_id();
		/**根据网关信息ID获得网关长号码*/
		GatewayVo gatewayVo = gatewayDao.getGatewayById(gateway_id);
		/**设置长号码信息*/
		routerVo.setSp_number(gatewayVo.getSp_number());
		
		/**判断是新增还是修改*/
		if(routerVo.getId() == 0){
			routerVo.setAdd_user(userInfoVo.getId());
			
			int count = routerDao.insertRouter(routerVo);
			if(count > 0){
				logger.info(String.format("[%s]添加[%s]渠道路由信息成功", userInfoVo.getLogin_username(),routerVo.getSp_number()));
			}else{
				logger.info(String.format("[%s]添加[%s]渠道路由信息失败", userInfoVo.getLogin_username(),routerVo.getSp_number()));
			}
			
		}else {
			routerVo.setUpdate_user(userInfoVo.getId());
			int count = routerDao.updateRouter(routerVo);
			if(count > 0){
				logger.info(String.format("[%s]更新[%s]渠道路由信息成功", userInfoVo.getLogin_username(),routerVo.getSp_number()));
			}else{
				logger.info(String.format("[%s]更新[%s]渠道路由信息失败", userInfoVo.getLogin_username(),routerVo.getSp_number()));
			}
		}
		
		actionPath = "router/getRouterList.action";
		return SUCCESS;
	}
	
	public String deleteRouter(){
		HttpServletRequest request = ServletActionContext.getRequest();
		UserInfoVo userInfoVo = (UserInfoVo) request.getAttribute("user");
		
		int count = routerDao.deleteRouter(routerVo.getId(), userInfoVo.getId());
		if(count > 0){
			logger.info(String.format("[%s]删除[%s]渠道路由信息成功", userInfoVo.getLogin_username(),routerVo.getSp_number()));
		}else{
			logger.info(String.format("[%s]删除[%s]渠道路由信息失败", userInfoVo.getLogin_username(),routerVo.getSp_number()));
		}
		actionPath = "router/getRouterList.action";
		return SUCCESS;
	}

	public IRouterDao getRouterDao() {
		return routerDao;
	}

	public void setRouterDao(IRouterDao routerDao) {
		this.routerDao = routerDao;
	}

	public IPartnerLowerDao getPartnerLowerDao() {
		return partnerLowerDao;
	}

	public void setPartnerLowerDao(IPartnerLowerDao partnerLowerDao) {
		this.partnerLowerDao = partnerLowerDao;
	}

	public IGatewayDao getGatewayDao() {
		return gatewayDao;
	}

	public void setGatewayDao(IGatewayDao gatewayDao) {
		this.gatewayDao = gatewayDao;
	}

	public List<RouterVo> getRouterVoList() {
		return routerVoList;
	}

	public void setRouterVoList(List<RouterVo> routerVoList) {
		this.routerVoList = routerVoList;
	}

	public List<PartnerLowerVo> getPartnerLowerVoList() {
		return partnerLowerVoList;
	}

	public void setPartnerLowerVoList(List<PartnerLowerVo> partnerLowerVoList) {
		this.partnerLowerVoList = partnerLowerVoList;
	}

	public List<GatewayVo> getGatewayVoList() {
		return gatewayVoList;
	}

	public void setGatewayVoList(List<GatewayVo> gatewayVoList) {
		this.gatewayVoList = gatewayVoList;
	}

	public RouterVo getRouterVo() {
		return routerVo;
	}

	public void setRouterVo(RouterVo routerVo) {
		this.routerVo = routerVo;
	}

	public String getActionPath() {
		return actionPath;
	}

	public void setActionPath(String actionPath) {
		this.actionPath = actionPath;
	}
}